<template>
  <page-view>
    <el-card>
      <template #header>
        <div class="font-bold">订单列表</div>
      </template>
      <el-form :model="queryFormData" :inline="true" class="flex">
        <div class="demo-date-picker">
          <div class="block">
            <span class="demonstration mr-10">时间</span>
            <el-date-picker v-model="value1" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" :size="size" />
          </div>
        </div>

        <el-form-item label="订单状态" class="ml-10">
          <el-select v-model="value" class="mr-[25px]" placeholder="请选择状态">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input placeholder="请输入用户ID" v-model="queryFormData.uid"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryData" :loading="isLoading">
            <el-icon>
              <Search />
            </el-icon>
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <div v-loading="isLoading">
        <!--要展示数据了-->
        <el-table :data="resultData.listData" border style="width: 100%" stripe max-height="550">
          <!-- 展开列表 -->
          <el-table-column type="expand">
            <template #default="{ row }">
              <el-descriptions style="box-sizing: border-box; padding: 10px" :column="3" size="small" border>
                <template #title>
                  <span style="color: #409eff; font-weight: bold">客户信息</span>
                </template>
                <el-descriptions-item label="用户昵称">
                  {{ row.userInfo.nickName }}
                </el-descriptions-item>
                <el-descriptions-item label="性别">
                  {{ row.userInfo.user_sex }}
                </el-descriptions-item>
                <el-descriptions-item label="手机号">
                  {{ row.userInfo.user_phone }}
                </el-descriptions-item>
                <el-descriptions-item label="邮箱">{{ row.userInfo.user_email }}
                </el-descriptions-item>
                <el-descriptions-item label="本次收货地址" :span="2">
                  {{ row.addressInfo.address }}
                </el-descriptions-item>
              </el-descriptions>

              <el-descriptions style="box-sizing: border-box; padding: 10px" :column="3" size="small" border>
                <template #title>
                  <span style="color: #409eff; font-weight: bold">订单详细信息</span>
                </template>
                <template v-for="(item, index) in row.orderDetailList">
                  <el-descriptions-item label="菜品名称" width="150">
                    {{ item.foodInfo.food_name }}
                  </el-descriptions-item>
                  <el-descriptions-item label="单价">{{ item.foodInfo.price }}
                  </el-descriptions-item>
                  <el-descriptions-item label="数量">{{
                      item.count
                  }}</el-descriptions-item>
                </template>
              </el-descriptions>
            </template>
          </el-table-column>

          <el-table-column label="ID" prop="id" width="90" align="center"></el-table-column>
          <el-table-column align="center" label="所属编号" prop="uid" width="150"></el-table-column>
          <el-table-column label="用户昵称" prop="userInfo.nickName" width="150" align="center"></el-table-column>
          <el-table-column label="订单生成时间" width="350" align="center">
            <template #default="{ row }">
              {{ formatDateTime(row.submit_time) }}
            </template>
          </el-table-column>
          <el-table-column label="订单总金额" prop="order_money" width="200" align="center"></el-table-column>
          <el-table-column label="菜品总数" prop="orderDetailList.length" width="200" align="center"></el-table-column>
          <el-table-column label="订单状态" prop="order_status" width="200" align="center"></el-table-column>
          <el-table-column label="付款时间" width="200" align="center">
            <template #default="{ row }">
              {{ formatDateTime(row.pay_time) }}
            </template>
          </el-table-column>

          <el-table-column label="发货时间" width="200" align="center">
            <template #default="{ row }">
              {{ formatDateTime(row.deliver_time) }}
            </template>
          </el-table-column>

          <el-table-column label="收货地址" prop="addressInfo.address" width="200" align="center">
          </el-table-column>

          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="{ row }">
              <el-popconfirm title="你确定要删除吗" @confirm="updateOrderToDeliver(row.id)">
                <template #reference>
                  <el-button type="primary" v-if="row.order_status == '已付款'" size="small">发货</el-button>

                  <el-button type="primary" v-else disabled size="small">发货</el-button>
                </template>

              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--页码-->
        <div class="flex flex-row justify-between items-center mt-[10px]">
          <div class="text-[14px]">
            当前第{{ queryFormData.pageIndex }}页，共{{
                resultData.pageCount
            }}页，共{{ resultData.totalCount }}条
          </div>
          <el-pagination background layout="prev, pager, next" :total="resultData.totalCount"
            @current-change="currentChange" />
        </div>
      </div>
    </el-card>
  </page-view>
</template>

<script setup>
import { reactive, ref } from "vue";
import { Search, CirclePlus } from "@element-plus/icons-vue";
import { formatDateTime } from "@/utils/DateTimeUtils";
import API from "@/utils/API/index.js";
import { ElNotification } from "element-plus";

import { onMounted, inject } from "vue";

const baseURL = inject("baseURL");
const isLoading = ref(false);

const size = ref("");
const value1 = ref("");

const shortcuts = [
  {
    text: "Last week",
    value: () => {
      const end_time = new Date();
      const start_time = new Date();
      start_time.setTime(start_time.getTime() - 3600 * 1000 * 24 * 7);
      return [start_time, end_time];
    },
  },
  {
    text: "Last month",
    value: () => {
      const end_time = new Date();
      const start_time = new Date();
      start_time.setTime(start_time.getTime() - 3600 * 1000 * 24 * 30);
      return [start_time, end_time];
    },
  },
  {
    text: "Last 3 months",
    value: () => {
      const end_time = new Date();
      const start_time = new Date();
      start_time.setTime(start_time.getTime() - 3600 * 1000 * 24 * 90);
      return [start_time, end_time];
    },
  },
];

const value = ref("");
const options = [
  {
    value: "未付款",
    label: "未付款",
  },
  {
    value: "已付款",
    label: "已付款",
  },
  {
    value: "已发货",
    label: "已发货",
  },
];
// 列表数据
const parentBorder = ref(false);
const childBorder = ref(false);
const tableData = [
  {
    state: "California",
    city: "San Francisco",
    address: "3650 21st St, San Francisco",
    zip: "CA 94114",
    family: [
      {
        name: "Jerry",
        state: "California",
        city: "San Francisco",
        address: "3650 21st St, San Francisco",
        zip: "CA 94114",
      },
    ],
  },
];

const queryFormData = reactive({
  start_time: "",
  end_time: "",
  pageIndex: 1,
  order_status: value,
  uid: "",
});

const resultData = reactive({
  listData: [],
  pageCount: 0,
  totalCount: 0,
});

//查询操作
const queryData = () => {
  isLoading.value = true;
  API.orderInfo
    .getListByPage(queryFormData)
    .then((result) => {
      console.log(result);
      resultData.listData = result.listData;
      resultData.pageCount = result.pageCount;
      resultData.totalCount = result.totalCount;
    })
    .finally(() => {
      isLoading.value = false;
    });
};

//页码发生变化
const currentChange = (index) => {
  queryFormData.pageIndex = index;
  queryData();
};

//发货操作
const updateOrderToDeliver = (id) => {
  console.log(id);
  API.orderInfo.updateOrderToDeliver({ id }).then(result => {
    ElNotification({
      type: "success",
      title: "发货成功",
      message: "当前用户发货成功"
    })
    //重新渲染页面
    queryData()
  }).catch(error => {
    ElNotification({
      type: "danger",
      title: "发货失败",
      message: "当前用户发货失败，请重试或联系管理员"
    })
  })
}


onMounted(() => {
  queryData();
});
</script>

<style scoped>

</style>
